#include "MainScreen.h"
#ifdef _WINDOWS
#include <windows.h>
#endif
#include <gl/gl.h>
#include <gl/glu.h>
#include "SoundManager.h"
#include "ResourceManager.h"
#include "MouseUtility.h"
#include "Global.h"

// Cac define cho cac button
#define MUSIC_ON 0
#define MUSIC_OFF 1
#define MAIN_MENU 2
#define REPLAY 3
#define HELP 4

int m_curSelect;		//Xac dinh button dang duoc chuot tro toi
bool m_isEndGame;		//Xac dinh khi game win hoac lose
bool m_isShowingHelp;	//Xac dinh khi nao hien tro giup

MainScreen::MainScreen(ScreenManager* _screenManager)
	:GameScreen(_screenManager)
{
	m_screenState = On;						
	m_box.m_pos = Vector3(4, 0, 4);			//Vi tri SkyBox ngay giua ban co
	m_box.m_size = Vector3(100, 100, 100);	
	m_camera = new Camera();
	m_camera->SetCameraPos(4, 8, 11,
		4, 0, 4,
		0, 1, 0);


#pragma region Sound_Play
	/// Phat am thanh nhac nen
	SoundManager::GetInst()->PlaySound("Resource/sound/Larva.mp3");
	m_isMusicOn = true;
	/// Phat am thanh man choi moi
	SoundManager::GetInst()->PlaySound("Resource/sound/Replay.mp3");
#pragma endregion 
	
#pragma region Create_Button_List
	
	m_listButton = vector<Button*>();

	/// Tao cac button cho Main menu
	// 0 Music On
	m_listButton.push_back(new Button("Resource/Image/SBT_MusicOn.png", 
		-0.75, -0.42, -1.0,  
		0.1, 0.1,
		0.5, 0.8, 0.5, 1));

	// 1 Music Off
	m_listButton.push_back(new Button("Resource/Image/SBT_MusicOff.png", 
		-0.75, -0.42, -1.0,  
		0.1, 0.1,
		0.5, 0.8, 0.5, 1));

	// 2 Main menu
	m_listButton.push_back(new Button("Resource/Image/SBT_MainMenu.png", 
		-0.75, -0.09, -1.0,
		0.1, 0.1,
		0.5, 0.8, 0.5, 1));

	// 3 Replay
	m_listButton.push_back(new Button("Resource/Image/SBT_Replay.png", 
		-0.75, -0.2, -1.0,  
		0.1, 0.1,
		0.5, 0.8, 0.5, 1));

	// 4 Help
	m_listButton.push_back(new Button("Resource/Image/SBT_Help.png", 
		-0.75, -0.31, -1.0,  
		0.1, 0.1,
		0.5, 0.8, 0.5, 1));
#pragma endregion

}

MainScreen::~MainScreen(void)
{
}

void MainScreen::Update(float _dt)
{
	SoundManager::GetInst()->PauseBgSound("Resource/sound/Larva.mp3", !m_isMusicOn);

	m_curSelect = -1;
	for(int i = 1;i < m_listButton.size();i++)
	{
		if(m_listButton[i]->IsMouseMoving(MouseUtility::GetInst()->getGLMouseX(), MouseUtility::GetInst()->getGLMouseY()))
		{
			m_curSelect = i;
			break;
		}
	}

	Othello1.Update(_dt);	//update main game
}

void MainScreen::Draw()
{
#pragma region Draw_3D_space
	
	glPushMatrix();

	/// Set thong tin camera
	gluLookAt(m_camera->m_pos.X, m_camera->m_pos.Y, m_camera->m_pos.Z,
		m_camera->m_view.X, m_camera->m_view.Y, m_camera->m_view.Z,
		m_camera->m_dir.X, m_camera->m_dir.Y, m_camera->m_dir.Z);
	
	m_box.Draw(m_camera->m_pos.X, m_camera->m_pos.Y, m_camera->m_pos.Z);

	Othello1.Draw(m_camera->m_pos.X, m_camera->m_pos.Y, m_camera->m_pos.Z,
		m_camera->m_view.X, m_camera->m_view.Y, m_camera->m_view.Z);

	glPopMatrix();

#pragma endregion Draw_3D_space
	
#pragma region Draw_2D_User_Interface

	glPushMatrix();
	glLoadIdentity();
	glDepthMask(false);

#pragma region Win_Lose_Pop_Up

	if(Othello1.m_isEndGame)
	{
		if(Othello1.m_humanChessCounter > Othello1.m_computerChessCounter)
		{
			m_glDraw.GLDrawPlane("Resource/Image/BG_Win.png",
				0, 0.37, -1,
				0.4, 0.19, 0,
				1, 1, 1, 1.0);
			if(!m_isEndGame)
			{
				SoundManager::GetInst()->PlaySound("Resource/sound/Win.mp3");
				m_isEndGame = true;
			}
		}
		else
		{
			m_glDraw.GLDrawPlane("Resource/Image/BG_Lose.png",
				0, 0.37, -1,
				0.4, 0.19, 0,
				1, 1, 1, 1.0);
			if(!m_isEndGame)
			{
				SoundManager::GetInst()->PlaySound("Resource/sound/Lose.mp3");
				m_isEndGame = true;
			}
		}

		m_listButton[MAIN_MENU]->SetButtonPos(0.3, 0.41, -1);
		m_listButton[REPLAY]->SetButtonPos(0.3, 0.3, -1);

	}

#pragma endregion Win_Lose_Pop_Up
	
#pragma region Score_Display_Background
	/// Khi toi luot choi ben nao thi ben do khong trong suot, ben con lai giam alpha con 0.5
	if(Othello1.m_turn == Othello1.COMPUTER)
	{
		m_glDraw.GLDrawPlane("Resource/Image/PU_ComputerTurn.png",
			-0.65, 0.295, -1.0, 
			0.195, 0.26, 0,
			1.0, 1.0, 1.0, 1);
		m_glDraw.GLDrawPlane("Resource/Image/PU_PlayerTurn.png",
			0.65, 0.295, -1.0, 
			0.195, 0.26, 0,
			1.0, 1.0, 1.0, 0.5);
	}
	else
	{
		m_glDraw.GLDrawPlane("Resource/Image/PU_ComputerTurn.png",
			-0.65, 0.295, -1.0, 
			0.195, 0.26, 0,
			1.0, 1.0, 1.0, 0.5);
		m_glDraw.GLDrawPlane("Resource/Image/PU_PlayerTurn.png",
			0.65, 0.295, -1.0, 
			0.195, 0.26, 0,
			1.0, 1.0, 1.0, 1);
	}

#pragma endregion Score_Display_Background

#pragma region Draw_Score

	glColor4f(1, 0, 0, 1);
	m_glDraw.GLDrawScore(Othello1.m_computerChessCounter, -0.65, 0.35, -1.0, 0.15, 0.15);
	glColor4f(0, 0, 1, 1);
	m_glDraw.GLDrawScore(Othello1.m_humanChessCounter, 0.65, 0.35, -1.0, 0.15, 0.15);
	glDepthMask(true);
	glPopMatrix();

#pragma endregion Draw_Score

#pragma region Draw_Button

	// Button On/Off chi ve 1 trong 2
	if(m_isMusicOn)
		m_listButton[MUSIC_ON]->DrawButton();
	else
		m_listButton[MUSIC_OFF]->DrawButton();

	//Ve nhung Button con lai
	for(int i = 2; i < m_listButton.size(); i++)
	{
		m_listButton[i]->DrawButton();
	}

#pragma endregion Draw_Button
	
#pragma region Draw_Help_Pop_Up

	/// Ve Help
	if(m_isShowingHelp)
	{
		m_glDraw.GLDrawPlane("Resource/Image/PU_Help.png",
			0.0, 0.0, -1,
			1.0, 0.5, 0,
			1, 1, 1, 0.8);
		/*m_DrawPlane.Draw("Resource/Image/PU_Help.png",
		0.0, 0.0, -1,
		1.0, 0.5, 0,
		1, 1, 1, 0.8);*/
	}

#pragma endregion Draw_Help_Pop_Up
	
#pragma endregion Draw_2D_User_Interface

}


void MainScreen::KeyDown(int keyCode)
{
#pragma region Rotate_Arrow_Key

	float angleY=0, angleX=0;

	//vector chi phuong cua camera
	Vector3 _vector = Vector3(m_camera->m_pos.X - m_camera->m_view.X, m_camera->m_pos.Y - m_camera->m_view.Y, m_camera->m_pos.Z - m_camera->m_view.Z);

	//goc xoay ngang cua camera
	angleX = atan(_vector.X / _vector.Z) * 180 / 3.14;

	if(abs(angleX) >= 45)
	{
		if(m_camera->m_pos.X < m_camera->m_view.X)
		{
			switch(keyCode)
			{
			case 37: //left
				Othello1.ChessMoveUp();
				break;
			case 39: //right
				Othello1.ChessMoveDown();
				break;
			case 38: //up
				Othello1.ChessMoveRight();
				break;
			case 40: //down
				Othello1.ChessMoveLeft();
				break;
			default:
				break;
			}
		}

		else
		{
			switch(keyCode)
			{
			case 37: //left
				Othello1.ChessMoveDown();
				break;
			case 39: //right
				Othello1.ChessMoveUp();
				break;
			case 38: //up
				Othello1.ChessMoveLeft();
				break;
			case 40: //down
				Othello1.ChessMoveRight();
				break;
			default:
				break;
			}
		}
	}
	else
	{
		if(m_camera->m_pos.Z > m_camera->m_view.Z)
		{
			switch(keyCode)
			{
			case 37: //left
				Othello1.ChessMoveLeft();
				break;
			case 39: //right
				Othello1.ChessMoveRight();
				break;
			case 38: //up
				Othello1.ChessMoveUp();
				break;
			case 40: //down
				Othello1.ChessMoveDown();
				break;
			default:
				break;
			}
		}
		else
		{
			switch(keyCode)
			{
			case 37: //left
				Othello1.ChessMoveRight();
				break;
			case 39: //right
				Othello1.ChessMoveLeft();
				break;
			case 38: //up
				Othello1.ChessMoveDown();
				break;
			case 40: //down
				Othello1.ChessMoveUp();
				break;
			default:
				break;
			}

		}

	}

#pragma endregion Rotate_Arrow_Key
	
#pragma region Rotate_Camera

	switch(keyCode)
	{
	case 65: //a
		//m_camera->RotateView(0.01);
		m_camera->RotatePositionH(0.05);
		break;
	case 68: //d
		//m_camera->RotateView(-0.01);
		m_camera->RotatePositionH(-0.05);
		break;
	case 87: //w
		//m_camera->RotatePositionV(0.05);
		m_camera->MoveCamera(-0.05);
		break;
	case 83: //s
		//m_camera->RotatePositionV(-0.05);
		m_camera->MoveCamera(0.05);
		break;
	default:
		break;
	}

#pragma endregion Rotate_Camera

#pragma region Chess_Down_&_Esc

	switch(keyCode)
	{
	case 32: //space
		if(Othello1.IsMoveValid(Othello1.m_selectionPos.X, Othello1.m_selectionPos.Z)) 
		{
			Othello1.ChessDown();
			Othello1.timeCounter=0;
		}
		break;
	case 27: //escape
		MenuScreen* menuScreen;
		menuScreen = new MenuScreen(m_screenManager);
		m_screenManager->AddScreen(menuScreen);
		m_screenState = Off;
		break;


	default:
		break;
	}

#pragma endregion Chess_Down_&_Esc
	
}

void MainScreen::MouseClick(int mouseCode)
{
	switch(mouseCode)
	{
	case 0://left
		switch (m_curSelect)
		{
		case MUSIC_OFF://on
			m_isMusicOn = !m_isMusicOn;
			if(!m_isMusicOn)
				Global::GetInst()->isMusicOn = false;
			else
			{
				Global::GetInst()->isMusicOn = true;
			}
			break;
		case MAIN_MENU://maingame
			MenuScreen* menuScreen;
			menuScreen = new MenuScreen(m_screenManager);
			m_screenManager->AddScreen(menuScreen);
			m_screenState = Off;
			SoundManager::GetInst()->PauseBgSound("Resource/sound/Larva.mp3", true);
			SoundManager::GetInst()->PlaySound("Resource/sound/SelectButton.mp3");
			break;
		case REPLAY://replay
			Reset();
			SoundManager::GetInst()->PlaySound("Resource/sound/Replay.mp3");
			break;
		case HELP://Help
			m_isShowingHelp = !m_isShowingHelp;
			break;
		default:
			break;
		}
		break;
	case 1: //right
		break;
	default:
		break;
	}

}

void MainScreen::Reset()
{
	Othello1.Reset();
	m_listButton[MAIN_MENU]->SetButtonPos(-0.75, -0.09, -1.0);
	m_listButton[REPLAY]->SetButtonPos(-0.75, -0.2, -1.0);
	m_isEndGame = false;
}